<template>
    <div style="width: 500px">
        <el-input
                placeholder="请输入部门名称进行搜索..."
                prefix-icon="el-icon-search"
                v-model="inputSelectText">
        </el-input>

        <el-tree
                :data="deps"
                :props="defaultProps"
                :filter-node-method="filterNode"
                :expand-on-click-node="false"
                ref="tree">
            <span class="custom-tree-node" slot-scope="{ node, data }"
                  style="display: flex;justify-content: space-between;width: 100%">
                <span>{{ data.name }}</span>
                <span>
                  <el-button
                          type="primary"
                          size="mini"
                          class="depBtn"
                          @click="() => showAddDep(data)">
                    添加部门
                  </el-button>
                  <el-button
                          type="danger"
                          size="mini"
                          class="depBtn"
                          @click="() => deleteDep(data)">
                    删除部门
                  </el-button>
                </span>
            </span>
        </el-tree>
        <el-dialog
                title="添加部门"
                :visible.sync="dialogVisible"
                width="30%"
                :before-close="handleClose">
            <div>
                <table>
                    <tr>
                        <td>
                            <el-tag>
                                上级部门;
                            </el-tag>
                        </td>
                        <td>{{pname}}</td>
                    </tr>
                    <tr>
                        <td>
                            <el-tag>
                                部门名称:
                            </el-tag>
                        </td>
                        <td>
                            <el-input v-model="addDep.name" placeholder="请输入部门名称..."></el-input>
                        </td>
                    </tr>
                </table>
            </div>
            <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="doAddDep">确 定</el-button>
          </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "DepartmentManager",
        data() {
            return {
                //搜索框绑定数据
                inputSelectText: '',
                //树形控件数据
                deps: [],
                defaultProps: {
                    children: 'children',
                    label: 'name',
                },
                dialogVisible: false,
                //添加部门数据
                addDep: {
                    //部门名
                    name: '',
                    //父部门id
                    parentId: -1,
                },
                //上级部门名称
                pname: '',

            }
        },
        methods: {
            /**
             * 初始化树形菜单数据
             */
            initDeps() {
                this.getRequest('/sys/basic/department/getAllDepartments').then(res => {
                    if (res) {
                        this.deps = res;
                    }
                })
            },

            filterNode(value, data) {
                if (!value) return true;
                return data.name.indexOf(value) !== -1;
            },

            /**
             * 展示添加部门
             */
            showAddDep(data) {
                this.pname = data.name;
                this.addDep.parentId = data.id;
                this.dialogVisible = true;
            },

            /**
             * 添加部门
             */
            doAddDep() {
                if (this.addDep.name) {
                    this.postRequest('/sys/basic/department/addDepartment', this.addDep).then(res => {
                        if (res) {
                            this.insertDep(this.deps, res.obj);
                            this.dialogVisible = false;
                            this.initAddDep();
                        }
                    })
                } else {
                    this.$message.error("请输入部门名称后再添加")
                }
            },

            /**
             * 初始化添加数据
             */
            initAddDep() {
                this.addDep = {
                    name: '',
                    parentId: '',
                };
                this.pname = '';
            },

            /**
             * 删除部门
             */
            deleteDep(data) {
                if (data.isParent) {
                    this.$message.error("父部门删除失败，请先删除子部门！！")
                } else {
                    this.$confirm('此操作将永久删除【' + data.name + '】部门, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        this.deleteRequest('/sys/basic/department/deleteDepartment/' + data.id).then(res => {
                            if (res) {
                                this.removeDepFromDeps(null, this.deps, data.id);
                            }
                        })
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消删除'
                        });
                    });
                }
            },
            /**
             * 向树形菜单中插入添加的部门数据
             * @param deps 树形菜单数据
             * @param addDep 添加的部门数据
             * */
            insertDep(deps, addDep) {
                for (let i = 0; i < deps.length; i++) {
                    let d = deps[i];
                    if (d.id === addDep.parentId) {
                        d.children = d.children.concat(addDep);
                        if (d.children.length > 0) {
                            d.isParent = true;
                        }
                        return;
                    } else {
                        this.initAddDep(d.children, addDep);
                    }
                }
            },

            /**
             * 将树形菜单中删除的的部门数据移除
             * @param deps 树形菜单数据
             * @param id 删除的部门id
             * */
            removeDepFromDeps(p, deps, id) {
                for (let i = 0; i < deps.length; i++) {
                    let d = deps[i];
                    if (d.id === id) {
                        deps.splice(i, 1);
                        if (deps.length === 0) {
                            p.isParent = false;
                        }
                        return;
                    } else {
                        this.removeDepFromDeps(d, d.children, id);
                    }
                }
            }

        },
        watch: {
            /**
             * 监视输入框的值
             */
            inputSelectText(val) {
                this.$refs.tree.filter(val);
            }
        },
        mounted() {
            this.initDeps();
        },

    }
</script>

<style>
    .depBtn {
        padding: 2px;
    }
</style>
